package cn.ffcs.uec.staffRecords.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Div;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.recordsApproval.vo.TaskVo;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staffRecords.component.bean.StaffRecordsListboxExtBean;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class StaffRecordsListboxExt extends Div implements IdSpace {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3784572447622302721L;

	/**
	 * zul.员工档案新增界面
	 */
	public static final String STAFF_RECORDS_LISTBOX_EXT_ZUL = "/pages_nmg/staffRecords/comp/staffRecords_listbox_ext.zul";
	
	/**
	 * zul.员工档案新增校验页面
	 */
	public static final String STAFF_RECORDS_ADDCONFIRM_ZUL = "/pages_nmg/staffRecords/comp/staffRecords_addConfirm.zul";
	
	/**
	 * 对应页面上的bean
	 */
	private StaffRecordsListboxExtBean bean = new StaffRecordsListboxExtBean();

	/**
	 * 人员基本信息实体类
	 */
	private UecStaffInfo uecStaffInfo = new UecStaffInfo();

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;
	
	
	/**
	 * 构造函数
	 */
	public StaffRecordsListboxExt() {
		Executions.createComponents(STAFF_RECORDS_LISTBOX_EXT_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.bindCombobox();
	}

	/**
	 * 新增
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */
	public void onAdd() throws SuspendNotAllowedException, Exception {
		// 新增员工前进行实名制校验
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", "addConfirm");
		Window window = (Window) Executions.createComponents(STAFF_RECORDS_ADDCONFIRM_ZUL, this, map);
		window.doModal();
		window.addEventListener("onOK", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getData() != null) {
					uecStaffInfo = (UecStaffInfo) event.getData();
					PubUtil.fillBeanFromPo(uecStaffInfo, bean);
				}
			}
		});
	}

	/**
	 * 保存
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public void onSave() throws Exception {
		PubUtil.fillPoFromBean(bean, uecStaffInfo);
		//保存数据，但是status_cd = 1100无限状态
		uecStaffInfo.addStatusCdIS1();
		//获取返回的staffId唯一值
		Long staffId = uecStaffInfo.getStaffId();
		
		ZkUtil.showInformation("保存成功,员工入职流程已经发起！", "提示信息");
		//PubUtil.clearComponent(this);
		/**
		 * 1、在此使用HttpClient访问服务器接口启动流程，但是要将上面的staffId带着
		 * 2、流程结束后，获取当前流程所带的值staffId
		 * 3、通过staffId来找到申请流程的记录，然后将这条记录的status_cd的状态变为1000
		 */

//		String procDefId = "staffEntry:1:4bd0361358e54f3b88fa31674e1e4838";
//		String formTheme = "盟市人员入职审批流程";
//		String urgent = "1";
//		String formContent = "入职人员:wangyiren";
//		String atiActCategoryId = "21";
		String startFlowJson = "{" + 
				   "formTheme: '盟市人员入职流程',"+
				   " formContent: '入职人员:wangyiren',"+
				   " procDefId: 'staffEntry:1:4bd0361358e54f3b88fa31674e1e4838',"+
				   " urgent: '1',"+
				   " atiActCategoryId: '21',"+
				   " procDefKey: '" + ApprovalUtil.STAFF_ENTRY + "',"+
				   " primaryId: " + staffId + 
				"}"	;
		String procInstId = ApprovalUtil.startApproval(startFlowJson);
		
		
		Map<String, Object> map = ApprovalUtil.getCurrentAssignNames(procInstId);
		TaskVo taskVo = (TaskVo) map.get("task");
		List<String> names = (List<String>) map.get("names");
		if(null != names && names.size() > 0) {
			for(String name : names) {
				if(PlatformUtil.getCurrentUser().getScreenName().equals(name)) {
					//签收任务
					ApprovalUtil.claim(taskVo.getTaskId());
					//完成任务
					ApprovalUtil.approvalPostHttp(null, "1", taskVo.getTaskId(), taskVo.getProcInsId());
				}
			}
		}

		
		
		
	}

	/**
	 * 重置
	 */
	public void onRest() {
		uecStaffInfo = null;
		PubUtil.clearComponent(this);
	}

	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		// 绑定证件类型
		 List<NodeVo> nodeVoIdTypeList = new ArrayList<NodeVo>();
		 String[][] idType = {{"0", "身份证"}};
		 for (int i = 0; i < idType.length; i++) {
		 NodeVo nodeVo = new NodeVo();
		 for (int j = 0; j < 1; j++) {
		 nodeVo.setId(idType[i][j]);
		 nodeVo.setName(idType[i][j + 1]);
		 }
		 nodeVoIdTypeList.add(nodeVo);
		 }
		 ListboxUtils.rendererForEdit(bean.getIdtype(), nodeVoIdTypeList);

		// 绑定性别sex:
		List<NodeVo> nodeVoSexList = new ArrayList<NodeVo>();
		String[][] sex = { { "1", "男" }, { "2", "女" } };
		for (int i = 0; i < sex.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(sex[i][j]);
				nodeVo.setName(sex[i][j + 1]);
			}
			nodeVoSexList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getSex(), nodeVoSexList);

		// 绑定民族nationality
		List<NodeVo> nodeVoNationalityList = new ArrayList<NodeVo>();
		String[][] nationality = { { "1001A11000000000063W", "汉族" }, { "1001A110000000000647", "侗族" },
				{ "1001A11000000000063Y", "回族" }, { "1001A110000000000646", "满族" }, { "1001A11000000000063X", "蒙古族" } };
		for (int i = 0; i < nationality.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(nationality[i][j]);
				nodeVo.setName(nationality[i][j + 1]);
			}
			nodeVoNationalityList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getNationality(), nodeVoNationalityList);

		// 婚姻状况marital
		List<NodeVo> nodeVoMaritalList = new ArrayList<NodeVo>();
		String[][] marital = { { "1001A1100000000005XQ", "未婚" }, { "1001A1100000000005XR", "已婚" },
				{ "1001A1100000000005XS", "再婚" }, { "1001A1100000000005XT", "离异" }, { "1001A1100000000005XU", "丧偶" } };
		for (int i = 0; i < marital.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(marital[i][j]);
				nodeVo.setName(marital[i][j + 1]);
			}
			nodeVoMaritalList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getMarital(), nodeVoMaritalList);

		// 健康状况
		List<NodeVo> nodeVoHealthList = new ArrayList<NodeVo>();
		String[][] health = { { "1001A1100000000005YD", "健康" }, { "1001A1100000000005YE", "一般" },
				{ "1001A1100000000005YF", "有慢性病" }, { "1001A1100000000005YG", "有生理缺陷" },
				{ "1001A1100000000005YH", "残疾" }, { "1001A1100000000005YI", "较差" } };
		for (int i = 0; i < health.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(health[i][j]);
				nodeVo.setName(health[i][j + 1]);
			}
			nodeVoHealthList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getHealth(), nodeVoHealthList);

		// 政治面貌
		List<NodeVo> nodeVoPolityList = new ArrayList<NodeVo>();
		String[][] polity = { { "1001A1100000000006D6", "中国共产党党员" }, { "1001A1100000000006D7", "中国共产党预备党员" },
				{ "1001A1100000000006D8", "中国共产主义青年团团员" }, { "1001A1100000000006DI", "群众" } };
		for (int i = 0; i < polity.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(polity[i][j]);
				nodeVo.setName(polity[i][j + 1]);
			}
			nodeVoPolityList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPolity(), nodeVoPolityList);

		// 学历edu
		List<NodeVo> nodeVoEduList = new ArrayList<NodeVo>();
		String[][] edu = { { "1001A1100000000006AN", "硕士研究生" }, { "1001A1100000000006AO", "研究生同等学历" },
				{ "1001A1100000000006AP", "大学本科" }, { "1001A1100000000006AQ", "专科" }, { "1001A1100000000006AR", "中专" },
				{ "1001A1100000000006AS", "高中" }, { "1001A1100000000006AT", "技工学校" }, { "1001A1100000000006AU", "初中" },
				{ "1001A1100000000006AV", "小学" }, { "1001A1100000000006AW", "无" },
				{ "1001A1100000000006AX", "大学普通班" } };
		for (int i = 0; i < edu.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(edu[i][j]);
				nodeVo.setName(edu[i][j + 1]);
			}
			nodeVoEduList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getEdu(), nodeVoEduList);

		// 学位pkDegree
		List<NodeVo> nodeVoPkDegreeList = new ArrayList<NodeVo>();
		String[][] pkDegree = { { "1001A1100000000006AY", "博士" }, { "1001A1100000000006AZ", "双硕士" },
				{ "1001A1100000000006B0", "硕士" }, { "1001A1100000000006B1", "双学士" }, { "1001A1100000000006B2", "学士" },
				{ "1001A1100000000006B3", "无" } };
		for (int i = 0; i < pkDegree.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(pkDegree[i][j]);
				nodeVo.setName(pkDegree[i][j + 1]);
			}
			nodeVoPkDegreeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPkDegree(), nodeVoPkDegreeList);

		// 户口性质characterrpr
		List<NodeVo> nodeVoCharacterrprList = new ArrayList<NodeVo>();
		String[][] characterrpr = { { "1001A1100000000005XM", "城镇" }, { "1001A1100000000005XN", "农村" } };
		for (int i = 0; i < characterrpr.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(characterrpr[i][j]);
				nodeVo.setName(characterrpr[i][j + 1]);
			}
			nodeVoCharacterrprList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getCharacterrpr(), nodeVoCharacterrprList);

		// 是否是历史成员
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getIshiskeypsn(), nodeVoIshiskeypsnList);
		// 是否是门店营业员
		ListboxUtils.rendererForEdit(bean.getIsshopassist(), nodeVoIshiskeypsnList);
		// 是否承包经营个人
		ListboxUtils.rendererForEdit(bean.getGlbdef14(), nodeVoIshiskeypsnList);

	}

}
